// // Copyright (c) 2009 All Right Reserved // // vl // // 2009-01-01 // Contains ... using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Diagnostics.Contracts; using System.Linq; using System.Xml.Linq; using JetBrains.Annotations; namespace LargoCommon.Music { /// /// Musical Block Changes. /// public sealed class MusicalChanges { #region Fields /// /// Musical Changes. /// private Collection changes; #endregion #region Constructors /// /// Initializes a new instance of the class. /// public MusicalChanges() { this.Changes = new Collection(); } /// /// Initializes a new instance of the class. /// /// The mark block model. public MusicalChanges(XContainer markBlockModel) { //// XElement Contract.Requires(markBlockModel != null); if (markBlockModel == null) { return; } //// EnergyChanges XElement xchanges = markBlockModel.Element("EnergyChanges"); if (xchanges != null) { foreach (var xchange in xchanges.Elements()) { EnergyChange change = new EnergyChange(xchange); this.Changes.Add(change); } } //// HarmonicChanges xchanges = markBlockModel.Element("HarmonicChanges"); if (xchanges != null) { foreach (var xchange in xchanges.Elements()) { var change = new HarmonicChange(xchange); this.Changes.Add(change); } } //// InstrumentChanges xchanges = markBlockModel.Element("InstrumentChanges"); if (xchanges != null) { foreach (var xchange in xchanges.Elements()) { var change = new InstrumentChange(xchange); this.Changes.Add(change); } } //// LoudnessChanges xchanges = markBlockModel.Element("LoudnessChanges"); if (xchanges != null) { foreach (var xchange in xchanges.Elements()) { var change = new LoudnessChange(xchange); this.Changes.Add(change); } } //// OctaveChanges xchanges = markBlockModel.Element("OctaveChanges"); if (xchanges != null) { foreach (var xchange in xchanges.Elements()) { var change = new OctaveChange(xchange); this.Changes.Add(change); } } //// StaffChanges xchanges = markBlockModel.Element("StaffChanges"); if (xchanges != null) { foreach (var xchange in xchanges.Elements()) { var change = new StaffChange(xchange); this.Changes.Add(change); } } //// TempoChanges xchanges = markBlockModel.Element("TempoChanges"); if (xchanges != null) { foreach (var xchange in xchanges.Elements()) { var change = new TempoChange(xchange); this.Changes.Add(change); } } //// TonalityChanges xchanges = markBlockModel.Element("TonalityChanges"); if (xchanges != null) { foreach (var xchange in xchanges.Elements()) { var change = new TonalityChange(xchange); this.Changes.Add(change); } } } #endregion #region Properties - Xml /// Gets Xml representation. /// Property description. public XElement GetXElement { get { XElement xlist = new XElement("Changes"); //// EnergyChanges XElement xchanges = new XElement("EnergyChanges"); foreach (var change in this.EnergyChanges.Where(change => change != null)) { var xchange = change.GetXElement; xchanges.Add(xchange); } xlist.Add(xchanges); //// HarmonicChanges xchanges = new XElement("HarmonicChanges"); foreach (var change in this.HarmonicChanges.Where(change => change != null)) { var xchange = change.GetXElement; xchanges.Add(xchange); } xlist.Add(xchanges); //// InstrumentChanges xchanges = new XElement("InstrumentChanges"); foreach (var change in this.InstrumentChanges.Where(change => change != null)) { var xchange = change.GetXElement; xchanges.Add(xchange); } xlist.Add(xchanges); //// LoudnessChanges xchanges = new XElement("LoudnessChanges"); foreach (var change in this.LoudnessChanges.Where(change => change != null)) { var xchange = change.GetXElement; xchanges.Add(xchange); } xlist.Add(xchanges); //// OctaveChanges xchanges = new XElement("OctaveChanges"); foreach (var change in this.OctaveChanges.Where(change => change != null)) { var xchange = change.GetXElement; xchanges.Add(xchange); } xlist.Add(xchanges); //// StaffChanges xchanges = new XElement("StaffChanges"); foreach (var change in this.StaffChanges.Where(change => change != null)) { var xchange = change.GetXElement; xchanges.Add(xchange); } xlist.Add(xchanges); //// TempoChanges xchanges = new XElement("TempoChanges"); foreach (var change in this.TempoChanges.Where(change => change != null)) { var xchange = change.GetXElement; xchanges.Add(xchange); } xlist.Add(xchanges); //// TonalityChanges xchanges = new XElement("TonalityChanges"); foreach (var change in this.TonalityChanges.Where(change => change != null)) { var xchange = change.GetXElement; xchanges.Add(xchange); } xlist.Add(xchanges); return xlist; } } #endregion #region Public properties /// /// Gets the musical changes. /// /// /// The musical changes. /// public Collection Changes { get { Contract.Ensures(Contract.Result>() != null); if (this.changes == null) { throw new InvalidOperationException(Localization.LocalizedMusic.String("Musical model has no changes.")); } return this.changes; } private set => this.changes = value ?? throw new ArgumentException(Localization.LocalizedMusic.String("Argument cannot be empty."), nameof(value)); } /// /// Gets the harmonic changes. /// /// Property description. public IEnumerable HarmonicChanges { get { if (this.changes == null) { return null; } var cs = (from c in this.changes where c.ChangeType == MusicalChangeType.Harmonic orderby c.BarNumber select c as HarmonicChange).ToList(); return cs; } } /// /// Gets the tempo changes. /// /// Property description. public IEnumerable TempoChanges { get { if (this.changes == null) { return null; } var cs = (from c in this.changes where c.ChangeType == MusicalChangeType.Tempo orderby c.BarNumber select c as TempoChange).ToList(); return cs; } } /// /// Gets the instrument changes. /// /// Property description. public IEnumerable InstrumentChanges { get { if (this.changes == null) { return null; } var cs = (from c in this.changes where c.ChangeType == MusicalChangeType.Instrument orderby c.BarNumber select c as InstrumentChange).ToList(); return cs; } } /// /// Gets the staff changes. /// /// Property description. public IEnumerable StaffChanges { get { if (this.changes == null) { return null; } var cs = (from c in this.changes where c.ChangeType == MusicalChangeType.Staff orderby c.BarNumber select c as StaffChange).ToList(); return cs; } } /// /// Gets the staff changes. /// /// Property description. public IEnumerable OctaveChanges { get { if (this.changes == null) { return null; } var cs = (from c in this.changes where c.ChangeType == MusicalChangeType.Octave orderby c.BarNumber select c as OctaveChange).ToList(); return cs; } } /// /// Gets the staff changes. /// /// Property description. public IEnumerable LoudnessChanges { get { if (this.changes == null) { return null; } var cs = (from c in this.changes where c.ChangeType == MusicalChangeType.Loudness orderby c.BarNumber select c as LoudnessChange).ToList(); return cs; } } /// /// Gets the energy changes. /// /// Property description. public IEnumerable EnergyChanges { get { if (this.changes == null) { return null; } var cs = (from c in this.changes where c.ChangeType == MusicalChangeType.Energy orderby c.BarNumber select c as EnergyChange).ToList(); return cs; } } /// /// Gets the tonality changes. /// /// Property description. public IEnumerable TonalityChanges { get { if (this.changes == null) { return null; } var cs = (from c in this.changes where c.ChangeType == MusicalChangeType.Tonality orderby c.BarNumber select c as TonalityChange).ToList(); return cs; } } #endregion #region Public methods /// /// Appends the copy of changes. /// /// The given changes. public void AppendCopyOfChanges(IEnumerable givenChanges) { //// BlockModel givenModel Contract.Requires(givenChanges != null); this.AppendChanges(givenChanges.Select(tmc => (AbstractChange)tmc.Clone())); } /// /// Removes the changes. /// /// Type of the change. [UsedImplicitly] public void RemoveChanges(MusicalChangeType changeType) { var cs = (from mc in this.Changes where mc.ChangeType == changeType select mc).ToList(); cs.ForEach(mc => this.Changes.Remove(mc)); } /// /// Find the last change in given line. /// /// The line index. /// Bar number. /// Type of the change. /// /// Returns value. /// public AbstractChange CurrentChange(int lineIndex, int barNumber, MusicalChangeType changeType) { IEnumerable musicChanges = (from c in this.Changes where c.LineIndex == lineIndex && c.BarNumber == barNumber && c.ChangeType == changeType orderby c.IsStop descending, c.MusicalLineType select c).ToList(); //// ReSharper disable PossibleMultipleEnumeration return !musicChanges.Any() ? null : musicChanges.Last(); //// ReSharper restore PossibleMultipleEnumeration } #endregion #region Private methods /// /// Appends the changes. /// /// The given changes. private void AppendChanges(IEnumerable givenChanges) { //// , BlockModel givenModel Contract.Requires(givenChanges != null); foreach (var change in givenChanges) { //// change.BlockModel = givenModel; this.Changes.Add(change); } } #endregion } }